package inutan.login.controller;

import inutan.common.config.LoginConfig;
import inutan.common.constant.Constant;
import inutan.common.generator.UserTokenCreator;
import inutan.common.message.ActionErrorEnum;
import inutan.login.service.LoginService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.ricks.common.json.JSONObject;
import org.ricks.common.net.RequestMethod;
import org.ricks.common.utils.StringUtils;
import org.ricks.ioc.anno.Autowired;
import org.ricks.ioc.anno.Controller;
import org.ricks.ioc.anno.RequestMapping;
import org.ricks.net.anno.Param;
import org.ricks.net.http.HttpRequest;

/**
 * @author chenwei
 * @Description: 测试接口
 * @date 2023/1/416:24
 */
@Controller
public class LoginController {

    Logger logger = LogManager.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "test")
    public String test(@Param("userToken") long userToken) {
//        loginService.test();
        return "ricks";
    }

    @RequestMapping(value = "login")
    public JSONObject login(HttpRequest request, @Param("openId") String openId, @Param("gameId") int gameId) {
        boolean test_env = LoginConfig.env.equals(Constant.ENV_TEST);
        ActionErrorEnum.illegaOpenId.assertTrue(!StringUtils.isBlank(openId));
        ActionErrorEnum.testEnvGameNotChoose.assertFalse(test_env && gameId <= 0);
        String remoteAddress = request.getRemoteAddr();
        String userTokenStr = gameId + "-" + UserTokenCreator.generate();
        return loginService.login(openId);
    }

    /**
     * 登录成功，绑定businessId
     * 绑定 或者 解绑
     * @param userId
     * @return
     */
    @RequestMapping(value = "changeBindBusiness",method = RequestMethod.POST)
    public void changeBindBusiness(int businessId ,long userId) {
        logger.info("玩家 userId:{} 正在改变游戏节点绑定关系 businessId:{} ",userId,businessId);
        loginService.changeBindBusiness(businessId,userId);
    }

    @RequestMapping(value = "getServerTime",method = RequestMethod.POST)
    public String getServerTime() {
        return System.currentTimeMillis() + "";
    }
}
